<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $username
 * @property string $password
 */
class Users extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 *
	 * @return user the static model class
	 */
	public static function model($className = __CLASS__)
	{
		return parent::model ( $className );
	}
	
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			// 自己的行为事件
			if($this->isNewRecord)
			{
				$this->createtime = $this->updatetime = $_SERVER['REQUEST_TIME'];
				// $this->author_id=Yii::app()->user->id;
			}
			else
				$this->updatetime = $_SERVER['REQUEST_TIME'];
			return true;
		}
		else
			return false;
	}
	
	protected function afterDelete()
	{
		parent::afterDelete();
		Comment::model()->deleteAll('post_id='.$this->id);
		Tag::model()->updateFrequency($this->tags, '');
	}
	
	protected function afterFind()
	{
		if ($this->depthid > 0 && $this->depthid == 178)
		{
			$this->depthname = 'aaaaa';
		} else {
			$this->depthname = '';
	    }
	    //echo '<BR><BR>afterFind:';
	    $this->cateid = 99;		
	}
	
	protected function cateId($cateid){
		//return $this->cateid;
	}
	/*protected function getDepthName(){
		// echo '<BR>getDepthName';
		//'getDepthName'.$this->depthid;
		return parent::getDepthName();
	}*/
	
	//protected function setDepthName(){
		//echo '<BR>setDepthName';
	//	return 'setDepthName';
	//}
	
	//protected function onCallBack(){}
	
	//protected function onDepthName($id){
	//	echo 'onDepthName($id)';
	//}
	/**
	 *
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}
	public function __get($name)
	{
		// parent::__get($name);
		// print_r ( $name );
		// echo '<BR>xx:';
		// var_dump($this->hasAttribute ( $name ));
		// print_r ( $this->$name );
		// parent::__get($name);
		// var_dump($this->hasAttribute ( $name ));
		if ($this->hasAttribute ( $name ) === false)
		{
			// echo '<BR>no hasAttributes:';
			// echo '<BR>__get($name):' . $name;
			// $this->$name .= 'ddd';
			// $this->setAttributes(array($name=>'dddd'), false);
			// var_dump($this->depthname);
			// echo ''
		} else
		{
			// echo '<BR>$name:' . $name;
		}
		if ($name == 'depthname')
		{
			// exit ( 'fdf' );
		}
		return parent::__get ( $name );
	}
	public function __set($name, $value)
	{
		if ($this->hasAttribute ( $name ) === false)
		{
			// echo '<BR>__set:' . $name . ',' . $value;
			
			$this->$name = $value;
		}
		// $this->$name = $value;
		parent::__set ( $name, $value );
		// $this->$name = $value;
	}
		
		/*
	 * public function setAttributes($attributes, $safe = true) {
	 * parent::setAttributes($attributes, $safe); }
	 */
	
	/**
	 *
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array (
				array (
						'username, password','required','className' => 'Users','criteria' => array (
								'condition' => 'active = 1' 
						) 
				),
				array('username','match', 'pattern'=>'/^[\w\s,]+$/',
                    'message'=>'Tags can only contain word characters.'),
				array('status', 'in', 'range'=>array(1,2,3))
				,array (
						'username','length','max' => 255 
				),array (
						'password','length','max' => 50 
				),array (
						'id, username, password','safe','on' => 'search' 
				) 
		);
	}
	
	/**
	 *
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array ();
	}
	
	/**
	 *
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array (
				'id' => 'Id','username' => 'Username','password' => 'Password' ,'depthid'=>'depthid');
	}
	public function scopes()
	{
		return array (
				// model()->published()->find...()
				'published' => array (
						'condition' => 'status=1' 
				),
				// model()->published()->recently()->find...()
				'recently' => array (
						'order' => 'timestamp DESC','limit' => '6' 
				) 
		);
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 *         based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria = new CDbCriteria ();
		
		$criteria->compare ( 'id', $this->id );
		
		$criteria->compare ( 'username', $this->username, true );
		
		$criteria->compare ( 'password', $this->password, true );
		
		return new CActiveDataProvider ( 'user', array (
				'criteria' => $criteria 
		) );
	}
	
	//其他代码
	
	/*public function beforeSave()
	{
		parent::beforeSave();
		//自己的行为事件
	}*/
}